package com.example.lib5.binaryTree.test;

import javax.management.relation.Role;

/**
 * 创建日期：2022/5/30 0030 16:23
 *
 * @author tony.sun
 * 类说明：
 */
public class BinaryDemoC {
    public static void main(String[] args) {
        BinaryC binaryC = new BinaryC();
        RoleC root = new RoleC(1,"唐三");
        RoleC role2 = new RoleC(2,"小舞");
        RoleC role3 = new RoleC(3,"千仞雪");
        RoleC role4 = new RoleC(4,"千寻疾");
        RoleC role5 = new RoleC(5,"比比东");
        root.setLeft(role2);
        root.setRight(role3);
        role3.setRight(role4);
        role3.setLeft(role5);
        binaryC.setRoot(root);
        System.out.println("----------------前序遍历----------------");
        binaryC.preOrder();
        System.out.println("----------------中序遍历----------------");
        binaryC.infixOrder();
        System.out.println("----------------后序遍历----------------");
        binaryC.postOrder();
        System.out.println("----------------前中后查询----------------");
        binaryC.preSearch(5);
        binaryC.infixSearch(5);
        binaryC.postSearch(5);
        System.out.println("----------------前删除----------------");
        binaryC.preOrder();
        binaryC.delete(5);
        System.out.println("删除后");
        binaryC.preOrder();
    }
}
//节点
class RoleC{
    private int no;
    private String name;
    private RoleC left;
    private RoleC right;

    public RoleC(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RoleC getLeft() {
        return left;
    }

    public void setLeft(RoleC left) {
        this.left = left;
    }

    public RoleC getRight() {
        return right;
    }

    public void setRight(RoleC right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "RoleC{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder() {
        System.out.println(this);
        if (this.left!=null) {
            this.left.preOrder();
        }
        if (this.right!=null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {
        if (this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    public void postOrder() {
        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public RoleC preSearch(int no) {
        System.out.println("查询了~~");
        if (this.no==no){
            return this;
        }
        RoleC temp=null;
        if (this.left!=null){
            temp=this.left.preSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        if (this.right!=null){
            temp=this.right.preSearch(no);
        }
        return temp;

    }
    public RoleC infixSearch(int no) {
        RoleC temp=null;
        if (this.left!=null){
            temp=this.left.infixSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        System.out.println("查询了~~");
        if (this.no==no){
            return this;
        }

        if (this.right!=null){
            temp=this.right.infixSearch(no);
        }
        return temp;

    }
    public RoleC postSearch(int no) {
        RoleC temp=null;
        if (this.left!=null){
            temp=this.left.postSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        if (this.right!=null){
            temp=this.right.postSearch(no);
        }
        if (temp!=null){
            return temp;
        }
        System.out.println("查询了~~");
        if (this.no==no){
            return this;
        }
        return temp;

    }

    public void delete(int no) {
        if (this.left!=null&&this.left.no==no) {
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no) {
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delete(no);
        }
        if (this.right!=null){
            this.right.delete(no);
        }
    }
}
//二叉树类
class BinaryC{

    private RoleC root;

    public void setRoot(RoleC root) {
        this.root=root;
    }

    public void preOrder() {
        if (root!=null){
            root.preOrder();
        }else {
            System.out.println("节点为空");
        }
    }
    public void infixOrder() {
        if (root!=null){
            root.infixOrder();
        }else {
            System.out.println("节点为空");
        }
    }
    public void postOrder() {
        if (root!=null){
            root.postOrder();
        }else {
            System.out.println("节点为空");
        }
    }

    public void preSearch(int no) {
        //判断节点是否为空
        if (root!=null) {
            RoleC roleC = root.preSearch(no);
            if (roleC!=null) {
                System.out.println(roleC);
            }else {
                System.out.println("找不到");
            }
        }else {
            System.out.println("节点为空");
        }
    }
    public void infixSearch(int no) {
        //判断节点是否为空
        if (root!=null) {
            RoleC roleC = root.infixSearch(no);
            if (roleC!=null) {
                System.out.println(roleC);
            }else {
                System.out.println("找不到");
            }
        }else {
            System.out.println("节点为空");
        }
    }
    public void postSearch(int no) {
        //判断节点是否为空
        if (root!=null) {
            RoleC roleC = root.postSearch(no);
            if (roleC!=null) {
                System.out.println(roleC);
            }else {
                System.out.println("找不到");
            }
        }else {
            System.out.println("节点为空");
        }
    }

    public void delete(int no) {
        if (root!=null) {
            if (root.getNo()==no) {
                root=null;
            }else {
                root.delete(no);
            }
        }else {
            System.out.println("节点为空");
        }
    }
}
